home *** CD-ROM | disk | FTP | other *** search
/ Mac-Source 1994 July / Mac-Source_July_1994.iso / C and C++ / Compilers⁄Interps / GCC-2.3.3r12 / Sources / genrecog.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-26  |  53.0 KB  |  1,808 lines  |  [TEXT/MPS ]

  1. /* Generate code from machine description to recognize rtl as insns.
  2.    Copyright (C) 1987, 1988, 1992 Free Software Foundation, Inc.
  3.  
  4. This file is part of GNU CC.
  5.  
  6. GNU CC is free software; you can redistribute it and/or modify
  7. it under the terms of the GNU General Public License as published by
  8. the Free Software Foundation; either version 2, or (at your option)
  9. any later version.
  10.  
  11. GNU CC is distributed in the hope that it will be useful,
  12. but WITHOUT ANY WARRANTY; without even the implied warranty of
  13. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  14. GNU General Public License for more details.
  15.  
  16. You should have received a copy of the GNU General Public License
  17. along with GNU CC; see the file COPYING.  If not, write to
  18. the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.  */
  19.  
  20.  
  21. /* This program is used to produce insn-recog.c, which contains
  22.    a function called `recog' plus its subroutines.
  23.    These functions contain a decision tree
  24.    that recognizes whether an rtx, the argument given to recog,
  25.    is a valid instruction.
  26.  
  27.    recog returns -1 if the rtx is not valid.
  28.    If the rtx is valid, recog returns a nonnegative number
  29.    which is the insn code number for the pattern that matched.
  30.    This is the same as the order in the machine description of the
  31.    entry that matched.  This number can be used as an index into various
  32.    insn_* tables, such as insn_template, insn_outfun, and insn_n_operands
  33.    (found in insn-output.c).
  34.  
  35.    The third argument to recog is an optional pointer to an int.
  36.    If present, recog will accept a pattern if it matches except for
  37.    missing CLOBBER expressions at the end.  In that case, the value
  38.    pointed to by the optional pointer will be set to the number of
  39.    CLOBBERs that need to be added (it should be initialized to zero by
  40.    the caller).  If it is set nonzero, the caller should allocate a
  41.    PARALLEL of the appropriate size, copy the initial entries, and call
  42.    add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.
  43.  
  44.    This program also generates the function `split_insns',
  45.    which returns 0 if the rtl could not be split, or
  46.    it returns the split rtl in a SEQUENCE.  */
  47.  
  48. #include <stdio.h>
  49. #include "hconfig.h"
  50. #include "rtl.h"
  51. #include "obstack.h"
  52.  
  53. static struct obstack obstack;
  54. struct obstack *rtl_obstack = &obstack;
  55.  
  56. #define obstack_chunk_alloc xmalloc
  57. #define obstack_chunk_free free
  58.  
  59. extern void free ();
  60. extern rtx read_rtx ();
  61.  
  62. /* Data structure for a listhead of decision trees.  The alternatives
  63.    to a node are kept in a doublely-linked list so we can easily add nodes
  64.    to the proper place when merging.  */
  65.  
  66. struct decision_head { struct decision *first, *last; };
  67.  
  68. /* Data structure for decision tree for recognizing
  69.    legitimate instructions.  */
  70.  
  71. struct decision
  72. {
  73.   int number;            /* Node number, used for labels */
  74.   char *position;        /* String denoting position in pattern */
  75.   RTX_CODE code;        /* Code to test for or UNKNOWN to suppress */
  76.   char ignore_code;        /* If non-zero, need not test code */
  77.   char ignore_mode;        /* If non-zero, need not test mode */
  78.   int veclen;            /* Length of vector, if nonzero */
  79.   enum machine_mode mode;    /* Machine mode of node */
  80.   char enforce_mode;        /* If non-zero, test `mode' */
  81.   char retest_code, retest_mode; /* See write_tree_1 */
  82.   int test_elt_zero_int;    /* Nonzero if should test XINT (rtl, 0) */
  83.   int elt_zero_int;        /* Required value for XINT (rtl, 0) */
  84.   int test_elt_one_int;        /* Nonzero if should test XINT (rtl, 1) */
  85.   int elt_one_int;        /* Required value for XINT (rtl, 1) */
  86.   int test_elt_zero_wide;    /* Nonzero if should test XWINT (rtl, 0) */
  87.   HOST_WIDE_INT elt_zero_wide;    /* Required value for XWINT (rtl, 0) */
  88.   char *tests;            /* If nonzero predicate to call */
  89.   int pred;            /* `preds' index of predicate or -1 */
  90.   char *c_test;            /* Additional test to perform */
  91.   struct decision_head success;    /* Nodes to test on success */
  92.   int insn_code_number;        /* Insn number matched, if success */
  93.   int num_clobbers_to_add;    /* Number of CLOBBERs to be added to pattern */
  94.   struct decision *next;    /* Node to test on failure */
  95.   struct decision *prev;    /* Node whose failure tests us */
  96.   struct decision *afterward;    /* Node to test on success, but failure of
  97.                    successor nodes */
  98.   int opno;            /* Operand number, if >= 0 */
  99.   int dupno;            /* Number of operand to compare against */
  100.   int label_needed;        /* Nonzero if label needed when writing tree */
  101.   int subroutine_number;    /* Number of subroutine this node starts */
  102. };
  103.  
  104. #ifdef MPW_C
  105. #define SUBROUTINE_THRESHOLD 40
  106. #else
  107. #define SUBROUTINE_THRESHOLD 50
  108. #endif
  109.  
  110. static int next_subroutine_number;
  111.  
  112. /* We can write two types of subroutines: One for insn recognition and
  113.    one to split insns.  This defines which type is being written.  */
  114.  
  115. enum routine_type {RECOG, SPLIT};
  116.  
  117. /* Next available node number for tree nodes.  */
  118.  
  119. static int next_number;
  120.  
  121. /* Next number to use as an insn_code.  */
  122.  
  123. static int next_insn_code;
  124.  
  125. /* Similar, but counts all expressions in the MD file; used for
  126.    error messages. */
  127.  
  128. static int next_index;
  129.  
  130. /* Record the highest depth we ever have so we know how many variables to
  131.    allocate in each subroutine we make.  */
  132.  
  133. static int max_depth;
  134.  
  135. /* This table contains a list of the rtl codes that can possibly match a
  136.    predicate defined in recog.c.  The function `not_both_true' uses it to
  137.    deduce that there are no expressions that can be matches by certain pairs
  138.    of tree nodes.  Also, if a predicate can match only one code, we can
  139.    hardwire that code into the node testing the predicate.  */
  140.  
  141. static struct pred_table
  142. {
  143.   char *name;
  144.   RTX_CODE codes[NUM_RTX_CODE];
  145. } preds[]
  146.   = {{"general_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
  147.               LABEL_REF, SUBREG, REG, MEM}},
  148. #ifdef PREDICATE_CODES
  149.      PREDICATE_CODES
  150. #endif
  151.      {"address_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
  152.               LABEL_REF, SUBREG, REG, MEM, PLUS, MINUS, MULT}},
  153.      {"register_operand", {SUBREG, REG}},
  154.      {"scratch_operand", {SCRATCH, REG}},
  155.      {"immediate_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
  156.                 LABEL_REF}},
  157.      {"const_int_operand", {CONST_INT}},
  158.      {"const_double_operand", {CONST_INT, CONST_DOUBLE}},
  159.      {"nonimmediate_operand", {SUBREG, REG, MEM}},
  160.      {"nonmemory_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
  161.                 LABEL_REF, SUBREG, REG}},
  162.      {"push_operand", {MEM}},
  163.      {"memory_operand", {SUBREG, MEM}},
  164.      {"indirect_operand", {SUBREG, MEM}},
  165.      {"comparison_operation", {EQ, NE, LE, LT, GE, LT, LEU, LTU, GEU, GTU}},
  166.      {"mode_independent_operand", {CONST_INT, CONST_DOUBLE, CONST, SYMBOL_REF,
  167.                    LABEL_REF, SUBREG, REG, MEM}}};
  168.  
  169. #define NUM_KNOWN_PREDS (sizeof preds / sizeof preds[0])
  170.  
  171. static int try_merge_1 ();
  172. static int no_same_mode ();
  173. static int same_codes ();
  174. static int same_modes ();
  175. char *xmalloc ();
  176. static struct decision *add_to_sequence ();
  177. static struct decision_head merge_trees ();
  178. static struct decision *try_merge_2 ();
  179. static void write_subroutine ();
  180. static void print_code ();
  181. static void clear_codes ();
  182. static void clear_modes ();
  183. static void change_state ();
  184. static void write_tree ();
  185. static char *copystr ();
  186. static char *concat ();
  187. static void fatal ();
  188. void fancy_abort ();
  189. static void mybzero ();
  190. static void mybcopy ();
  191.  
  192. /* Construct and return a sequence of decisions
  193.    that will recognize INSN.
  194.  
  195.    TYPE says what type of routine we are recognizing (RECOG or SPLIT).  */
  196.  
  197. static struct decision_head
  198. make_insn_sequence (insn, type)
  199.      rtx insn;
  200.      enum routine_type type;
  201. {
  202.   rtx x;
  203.   char *c_test = XSTR (insn, type == RECOG ? 2 : 1);
  204.   struct decision *last;
  205.   struct decision_head head;
  206.  
  207.   if (XVECLEN (insn, type == RECOG) == 1)
  208.     x = XVECEXP (insn, type == RECOG, 0);
  209.   else
  210.     {
  211.       x = rtx_alloc (PARALLEL);
  212.       XVEC (x, 0) = XVEC (insn, type == RECOG);
  213.       PUT_MODE (x, VOIDmode);
  214.     }
  215.  
  216.   last = add_to_sequence (x, &head, "");
  217.  
  218.   if (c_test[0])
  219.     last->c_test = c_test;
  220.   last->insn_code_number = next_insn_code;
  221.   last->num_clobbers_to_add = 0;
  222.  
  223.   /* If this is not a DEFINE_SPLIT and X is a PARALLEL, see if it ends with a
  224.      group of CLOBBERs of (hard) registers or MATCH_SCRATCHes.  If so, set up
  225.      to recognize the pattern without these CLOBBERs.  */
  226.  
  227.   if (type == RECOG && GET_CODE (x) == PARALLEL)
  228.     {
  229.       int i;
  230.  
  231.       for (i = XVECLEN (x, 0); i > 0; i--)
  232.     if (GET_CODE (XVECEXP (x, 0, i - 1)) != CLOBBER
  233.         || (GET_CODE (XEXP (XVECEXP (x, 0, i - 1), 0)) != REG
  234.         && GET_CODE (XEXP (XVECEXP (x, 0, i - 1), 0)) != MATCH_SCRATCH))
  235.       break;
  236.  
  237.       if (i != XVECLEN (x, 0))
  238.     {
  239.       rtx new;
  240.       struct decision_head clobber_head;
  241.  
  242.       if (i == 1)
  243.         new = XVECEXP (x, 0, 0);
  244.       else
  245.         {
  246.           int j;
  247.  
  248.           new = rtx_alloc (PARALLEL);
  249.           XVEC (new, 0) = rtvec_alloc (i);
  250.           for (j = i - 1; j >= 0; j--)
  251.         XVECEXP (new, 0, j) = XVECEXP (x, 0, j);
  252.         }
  253.  
  254.       last = add_to_sequence (new, &clobber_head, "");
  255.  
  256.       if (c_test[0])
  257.         last->c_test = c_test;
  258.       last->insn_code_number = next_insn_code;
  259.       last->num_clobbers_to_add = XVECLEN (x, 0) - i;
  260.  
  261.       head = merge_trees (head, clobber_head);
  262.     }
  263.     }
  264.  
  265.   next_insn_code++;
  266.  
  267.   if (type == SPLIT)
  268.     /* Define the subroutine we will call below and emit in genemit.  */
  269.     printf ("extern rtx gen_split_%d ();\n", last->insn_code_number);
  270.  
  271.   return head;
  272. }
  273.  
  274. /* Create a chain of nodes to verify that an rtl expression matches
  275.    PATTERN.
  276.  
  277.    LAST is a pointer to the listhead in the previous node in the chain (or
  278.    in the calling function, for the first node).
  279.  
  280.    POSITION is the string representing the current position in the insn.
  281.  
  282.    A pointer to the final node in the chain is returned.  */
  283.  
  284. static struct decision *
  285. add_to_sequence (pattern, last, position)
  286.      rtx pattern;
  287.      struct decision_head *last;
  288.      char *position;
  289. {
  290.   register RTX_CODE code;
  291.   register struct decision *new
  292.     = (struct decision *) xmalloc (sizeof (struct decision));
  293.   struct decision *this;
  294.   char *newpos;
  295.   register char *fmt;
  296.   register int i;
  297.   int depth = strlen (position);
  298.   int len;
  299.  
  300.   if (depth > max_depth)
  301.     max_depth = depth;
  302.  
  303.   new->number = next_number++;
  304.   new->position = copystr (position);
  305.   new->ignore_code = 0;
  306.   new->ignore_mode = 0;
  307.   new->enforce_mode = 1;
  308.   new->retest_code = new->retest_mode = 0;
  309.   new->veclen = 0;
  310.   new->test_elt_zero_int = 0;
  311.   new->test_elt_one_int = 0;
  312.   new->test_elt_zero_wide = 0;
  313.   new->elt_zero_int = 0;
  314.   new->elt_one_int = 0;
  315.   new->elt_zero_wide = 0;
  316.   new->tests = 0;
  317.   new->pred = -1;
  318.   new->c_test = 0;
  319.   new->success.first = new->success.last = 0;
  320.   new->insn_code_number = -1;
  321.   new->num_clobbers_to_add = 0;
  322.   new->next = 0;
  323.   new->prev = 0;
  324.   new->afterward = 0;
  325.   new->opno = -1;
  326.   new->dupno = -1;
  327.   new->label_needed = 0;
  328.   new->subroutine_number = 0;
  329.  
  330.   this = new;
  331.  
  332.   last->first = last->last = new;
  333.  
  334.   newpos = (char *) alloca (depth + 2);
  335.   strcpy (newpos, position);
  336.   newpos[depth + 1] = 0;
  337.  
  338.  restart:
  339.  
  340.   new->mode = GET_MODE (pattern);
  341.   new->code = code = GET_CODE (pattern);
  342.  
  343.   switch (code)
  344.     {
  345.     case MATCH_OPERAND:
  346.     case MATCH_SCRATCH:
  347.     case MATCH_OPERATOR:
  348.     case MATCH_PARALLEL:
  349.       new->opno = XINT (pattern, 0);
  350.       new->code = (code == MATCH_PARALLEL ? PARALLEL : UNKNOWN);
  351.       new->enforce_mode = 0;
  352.  
  353.       if (code == MATCH_SCRATCH)
  354.     new->tests = "scratch_operand";
  355.       else
  356.     new->tests = XSTR (pattern, 1);
  357.  
  358.       if (*new->tests == 0)
  359.     new->tests = 0;
  360.  
  361.       /* See if we know about this predicate and save its number.  If we do,
  362.      and it only accepts one code, note that fact.  The predicate
  363.      `const_int_operand' only tests for a CONST_INT, so if we do so we
  364.      can avoid calling it at all.
  365.  
  366.      Finally, if we know that the predicate does not allow CONST_INT, we
  367.      know that the only way the predicate can match is if the modes match
  368.      (here we use the kluge of relying on the fact that "address_operand"
  369.      accepts CONST_INT; otherwise, it would have to be a special case),
  370.      so we can test the mode (but we need not).  This fact should
  371.      considerably simplify the generated code.  */
  372.  
  373.       if (new->tests)
  374.     for (i = 0; i < NUM_KNOWN_PREDS; i++)
  375.       if (! strcmp (preds[i].name, new->tests))
  376.         {
  377.           int j;
  378.           int allows_const_int = 0;
  379.  
  380.           new->pred = i;
  381.  
  382.           if (preds[i].codes[1] == 0 && new->code == UNKNOWN)
  383.         {
  384.           new->code = preds[i].codes[0];
  385.           if (! strcmp ("const_int_operand", new->tests))
  386.             new->tests = 0, new->pred = -1;
  387.         }
  388.  
  389.           for (j = 0; j < NUM_RTX_CODE && preds[i].codes[j] != 0; j++)
  390.         if (preds[i].codes[j] == CONST_INT)
  391.           allows_const_int = 1;
  392.  
  393.           if (! allows_const_int)
  394.         new->enforce_mode = new->ignore_mode= 1;
  395.  
  396.           break;
  397.         }
  398.  
  399.       if (code == MATCH_OPERATOR || code == MATCH_PARALLEL)
  400.     {
  401.       for (i = 0; i < XVECLEN (pattern, 2); i++)
  402.         {
  403.           newpos[depth] = i + (code == MATCH_OPERATOR ? '0': 'a');
  404.           new = add_to_sequence (XVECEXP (pattern, 2, i),
  405.                      &new->success, newpos);
  406.         }
  407.     }
  408.  
  409.       return new;
  410.  
  411.     case MATCH_OP_DUP:
  412.       new->opno = XINT (pattern, 0);
  413.       new->dupno = XINT (pattern, 0);
  414.       new->code = UNKNOWN;
  415.       new->tests = 0;
  416.       for (i = 0; i < XVECLEN (pattern, 1); i++)
  417.     {
  418.       newpos[depth] = i + '0';
  419.       new = add_to_sequence (XVECEXP (pattern, 1, i),
  420.                  &new->success, newpos);
  421.     }
  422.       return new;
  423.  
  424.     case MATCH_DUP:
  425.     case MATCH_PAR_DUP:
  426.       new->dupno = XINT (pattern, 0);
  427.       new->code = UNKNOWN;
  428.       new->enforce_mode = 0;
  429.       return new;
  430.  
  431.     case ADDRESS:
  432.       pattern = XEXP (pattern, 0);
  433.       goto restart;
  434.  
  435.     case SET:
  436.       newpos[depth] = '0';
  437.       new = add_to_sequence (SET_DEST (pattern), &new->success, newpos);
  438.       this->success.first->enforce_mode = 1;
  439.       newpos[depth] = '1';
  440.       new = add_to_sequence (SET_SRC (pattern), &new->success, newpos);
  441.  
  442.       /* If set are setting CC0 from anything other than a COMPARE, we
  443.      must enforce the mode so that we do not produce ambiguous insns.  */
  444.       if (GET_CODE (SET_DEST (pattern)) == CC0
  445.       && GET_CODE (SET_SRC (pattern)) != COMPARE)
  446.     this->success.first->enforce_mode = 1;
  447.       return new;
  448.  
  449.     case SIGN_EXTEND:
  450.     case ZERO_EXTEND:
  451.     case STRICT_LOW_PART:
  452.       newpos[depth] = '0';
  453.       new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
  454.       this->success.first->enforce_mode = 1;
  455.       return new;
  456.  
  457.     case SUBREG:
  458.       this->test_elt_one_int = 1;
  459.       this->elt_one_int = XINT (pattern, 1);
  460.       newpos[depth] = '0';
  461.       new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
  462.       this->success.first->enforce_mode = 1;
  463.       return new;
  464.  
  465.     case ZERO_EXTRACT:
  466.     case SIGN_EXTRACT:
  467.       newpos[depth] = '0';
  468.       new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
  469.       this->success.first->enforce_mode = 1;
  470.       newpos[depth] = '1';
  471.       new = add_to_sequence (XEXP (pattern, 1), &new->success, newpos);
  472.       newpos[depth] = '2';
  473.       new = add_to_sequence (XEXP (pattern, 2), &new->success, newpos);
  474.       return new;
  475.  
  476.     case EQ:   case NE:   case LE:   case LT:   case GE:  case GT:
  477.     case LEU:  case LTU:  case GEU:  case GTU:
  478.       /* If the first operand is (cc0), we don't have to do anything
  479.      special.  */
  480.       if (GET_CODE (XEXP (pattern, 0)) == CC0)
  481.     break;
  482.  
  483.       /* ... fall through ... */
  484.       
  485.     case COMPARE:
  486.       /* Enforce the mode on the first operand to avoid ambiguous insns.  */
  487.       newpos[depth] = '0';
  488.       new = add_to_sequence (XEXP (pattern, 0), &new->success, newpos);
  489.       this->success.first->enforce_mode = 1;
  490.       newpos[depth] = '1';
  491.       new = add_to_sequence (XEXP (pattern, 1), &new->success, newpos);
  492.       return new;
  493.     }
  494.  
  495.   fmt = GET_RTX_FORMAT (code);
  496.   len = GET_RTX_LENGTH (code);
  497.   for (i = 0; i < len; i++)
  498.     {
  499.       newpos[depth] = '0' + i;
  500.       if (fmt[i] == 'e' || fmt[i] == 'u')
  501.     new = add_to_sequence (XEXP (pattern, i), &new->success, newpos);
  502.       else if (fmt[i] == 'i' && i == 0)
  503.     {
  504.       this->test_elt_zero_int = 1;
  505.       this->elt_zero_int = XINT (pattern, i);
  506.     }
  507.       else if (fmt[i] == 'i' && i == 1)
  508.     {
  509.       this->test_elt_one_int = 1;
  510.       this->elt_one_int = XINT (pattern, i);
  511.     }
  512.       else if (fmt[i] == 'w' && i == 0)
  513.     {
  514.       this->test_elt_zero_wide = 1;
  515.       this->elt_zero_wide = XWINT (pattern, i);
  516.     }
  517.       else if (fmt[i] == 'E')
  518.     {
  519.       register int j;
  520.       /* We do not handle a vector appearing as other than
  521.          the first item, just because nothing uses them
  522.          and by handling only the special case
  523.          we can use one element in newpos for either
  524.          the item number of a subexpression
  525.          or the element number in a vector.  */
  526.       if (i != 0)
  527.         abort ();
  528.       this->veclen = XVECLEN (pattern, i);
  529.       for (j = 0; j < XVECLEN (pattern, i); j++)
  530.         {
  531.           newpos[depth] = 'a' + j;
  532.           new = add_to_sequence (XVECEXP (pattern, i, j),
  533.                      &new->success, newpos);
  534.         }
  535.     }
  536.       else if (fmt[i] != '0')
  537.     abort ();
  538.     }
  539.   return new;
  540. }
  541.  
  542. /* Return 1 if we can prove that there is no RTL that can match both
  543.    D1 and D2.  Otherwise, return 0 (it may be that there is an RTL that
  544.    can match both or just that we couldn't prove there wasn't such an RTL).
  545.  
  546.    TOPLEVEL is non-zero if we are to only look at the top level and not
  547.    recursively descend.  */
  548.  
  549. static int
  550. not_both_true (d1, d2, toplevel)
  551.      struct decision *d1, *d2;
  552.      int toplevel;
  553. {
  554.   struct decision *p1, *p2;
  555.  
  556.   /* If they are both to test modes and the modes are different, they aren't
  557.      both true.  Similarly for codes, integer elements, and vector lengths. */
  558.  
  559.   if ((d1->enforce_mode && d2->enforce_mode
  560.        && d1->mode != VOIDmode && d2->mode != VOIDmode && d1->mode != d2->mode)
  561.       || (d1->code != UNKNOWN && d2->code != UNKNOWN && d1->code != d2->code)
  562.       || (d1->test_elt_zero_int && d2->test_elt_zero_int
  563.       && d1->elt_zero_int != d2->elt_zero_int)
  564.       || (d1->test_elt_one_int && d2->test_elt_one_int
  565.       && d1->elt_one_int != d2->elt_one_int)
  566.       || (d1->test_elt_zero_wide && d2->test_elt_zero_wide
  567.       && d1->elt_zero_wide != d2->elt_zero_wide)
  568.       || (d1->veclen && d2->veclen && d1->veclen != d2->veclen))
  569.     return 1;
  570.  
  571.   /* If either is a wild-card MATCH_OPERAND without a predicate, it can match
  572.      absolutely anything, so we can't say that no intersection is possible.
  573.      This case is detected by having a zero TESTS field with a code of
  574.      UNKNOWN.  */
  575.  
  576.   if ((d1->tests == 0 && d1->code == UNKNOWN)
  577.       || (d2->tests == 0 && d2->code == UNKNOWN))
  578.     return 0;
  579.  
  580.   /* If either has a predicate that we know something about, set things up so
  581.      that D1 is the one that always has a known predicate.  Then see if they
  582.      have any codes in common.  */
  583.  
  584.   if (d1->pred >= 0 || d2->pred >= 0)
  585.     {
  586.       int i, j;
  587.  
  588.       if (d2->pred >= 0)
  589.     p1 = d1, d1 = d2, d2 = p1;
  590.  
  591.       /* If D2 tests an explicit code, see if it is in the list of valid codes
  592.      for D1's predicate.  */
  593.       if (d2->code != UNKNOWN)
  594.     {
  595.       for (i = 0; i < NUM_RTX_CODE && preds[d1->pred].codes[i] != 0; i++)
  596.         if (preds[d1->pred].codes[i] == d2->code)
  597.           break;
  598.  
  599.       if (preds[d1->pred].codes[i] == 0)
  600.         return 1;
  601.     }
  602.  
  603.       /* Otherwise see if the predicates have any codes in common.  */
  604.  
  605.       else if (d2->pred >= 0)
  606.     {
  607.       for (i = 0; i < NUM_RTX_CODE && preds[d1->pred].codes[i] != 0; i++)
  608.         {
  609.           for (j = 0; j < NUM_RTX_CODE; j++)
  610.         if (preds[d2->pred].codes[j] == 0
  611.             || preds[d2->pred].codes[j] == preds[d1->pred].codes[i])
  612.           break;
  613.  
  614.           if (preds[d2->pred].codes[j] != 0)
  615.         break;
  616.         }
  617.  
  618.       if (preds[d1->pred].codes[i] == 0)
  619.         return 1;
  620.     }
  621.     }
  622.  
  623.   /* If we got here, we can't prove that D1 and D2 cannot both be true.
  624.      If we are only to check the top level, return 0.  Otherwise, see if
  625.      we can prove that all choices in both successors are mutually
  626.      exclusive.  If either does not have any successors, we can't prove
  627.      they can't both be true.  */
  628.  
  629.   if (toplevel || d1->success.first == 0 || d2->success.first == 0)
  630.     return 0;
  631.  
  632.   for (p1 = d1->success.first; p1; p1 = p1->next)
  633.     for (p2 = d2->success.first; p2; p2 = p2->next)
  634.       if (! not_both_true (p1, p2, 0))
  635.     return 0;
  636.  
  637.   return 1;
  638. }
  639.  
  640. /* Assuming that we can reorder all the alternatives at a specific point in
  641.    the tree (see discussion in merge_trees), we would prefer an ordering of
  642.    nodes where groups of consecutive nodes test the same mode and, within each
  643.    mode, groups of nodes test the same code.  With this order, we can
  644.    construct nested switch statements, the inner one to test the code and
  645.    the outer one to test the mode.
  646.  
  647.    We would like to list nodes testing for specific codes before those
  648.    that test predicates to avoid unnecessary function calls.  Similarly,
  649.    tests for specific modes should precede nodes that allow any mode.
  650.  
  651.    This function returns the merit (with 0 being the best) of inserting
  652.    a test involving the specified MODE and CODE after node P.  If P is
  653.    zero, we are to determine the merit of inserting the test at the front
  654.    of the list.  */
  655.  
  656. static int
  657. position_merit (p, mode, code)
  658.      struct decision *p;
  659.      enum machine_mode mode;
  660.      RTX_CODE code;
  661. {
  662.   enum machine_mode p_mode;
  663.  
  664.   /* The only time the front of the list is anything other than the worst
  665.      position is if we are testing a mode that isn't VOIDmode.  */
  666.   if (p == 0)
  667.     return mode == VOIDmode ? 3 : 2;
  668.  
  669.   p_mode = p->enforce_mode ? p->mode : VOIDmode;
  670.  
  671.   /* The best case is if the codes and modes both match.  */
  672.   if (p_mode == mode && p->code== code)
  673.     return 0;
  674.  
  675.   /* If the codes don't match, the next best case is if the modes match.
  676.      In that case, the best position for this node depends on whether
  677.      we are testing for a specific code or not.  If we are, the best place
  678.      is after some other test for an explicit code and our mode or after
  679.      the last test in the previous mode if every test in our mode is for
  680.      an unknown code.
  681.  
  682.      If we are testing for UNKNOWN, then the next best case is at the end of
  683.      our mode.  */
  684.  
  685.   if ((code != UNKNOWN
  686.        && ((p_mode == mode && p->code != UNKNOWN)
  687.        || (p_mode != mode && p->next
  688.            && (p->next->enforce_mode ? p->next->mode : VOIDmode) == mode
  689.            && (p->next->code == UNKNOWN))))
  690.       || (code == UNKNOWN && p_mode == mode
  691.       && (p->next == 0
  692.           || (p->next->enforce_mode ? p->next->mode : VOIDmode) != mode)))
  693.     return 1;
  694.  
  695.   /* The third best case occurs when nothing is testing MODE.  If MODE
  696.      is not VOIDmode, then the third best case is after something of any
  697.      mode that is not VOIDmode.  If we are testing VOIDmode, the third best
  698.      place is the end of the list.  */
  699.  
  700.   if (p_mode != mode
  701.       && ((mode != VOIDmode && p_mode != VOIDmode)
  702.       || (mode == VOIDmode && p->next == 0)))
  703.     return 2;
  704.  
  705.   /* Otherwise, we have the worst case.  */
  706.   return 3;
  707. }
  708.  
  709. /* Merge two decision tree listheads OLDH and ADDH,
  710.    modifying OLDH destructively, and return the merged tree.  */
  711.  
  712. static struct decision_head
  713. merge_trees (oldh, addh)
  714.      register struct decision_head oldh, addh;
  715. {
  716.   struct decision *add, *next;
  717.  
  718.   if (oldh.first == 0)
  719.     return addh;
  720.  
  721.   if (addh.first == 0)
  722.     return oldh;
  723.  
  724.   /* If we are adding things at different positions, something is wrong.  */
  725.   if (strcmp (oldh.first->position, addh.first->position))
  726.     abort ();
  727.  
  728.   for (add = addh.first; add; add = next)
  729.     {
  730.       enum machine_mode add_mode = add->enforce_mode ? add->mode : VOIDmode;
  731.       struct decision *best_position = 0;
  732.       int best_merit = 4;
  733.       struct decision *old;
  734.  
  735.       next = add->next;
  736.  
  737.       /* The semantics of pattern matching state that the tests are done in
  738.      the order given in the MD file so that if an insn matches two
  739.      patterns, the first one will be used.  However, in practice, most,
  740.      if not all, patterns are unambiguous so that their order is 
  741.      independent.  In that case, we can merge identical tests and
  742.      group all similar modes and codes together.
  743.  
  744.      Scan starting from the end of OLDH until we reach a point
  745.      where we reach the head of the list or where we pass a pattern
  746.      that could also be true if NEW is true.  If we find an identical
  747.      pattern, we can merge them.  Also, record the last node that tests
  748.      the same code and mode and the last one that tests just the same mode.
  749.  
  750.      If we have no match, place NEW after the closest match we found.  */
  751.      
  752.       for (old = oldh.last; old; old = old->prev)
  753.     {
  754.       int our_merit;
  755.  
  756.       /* If we don't have anything to test except an additional test,
  757.          do not consider the two nodes equal.  If we did, the test below
  758.          would cause an infinite recursion.  */
  759.       if (old->tests == 0 && old->test_elt_zero_int == 0
  760.           && old->test_elt_one_int == 0 && old->veclen == 0
  761.           && old->test_elt_zero_wide == 0
  762.           && old->dupno == -1 && old->mode == VOIDmode
  763.           && old->code == UNKNOWN
  764.           && (old->c_test != 0 || add->c_test != 0))
  765.         ;
  766.  
  767.       else if ((old->tests == add->tests
  768.             || (old->pred >= 0 && old->pred == add->pred)
  769.             || (old->tests && add->tests
  770.             && !strcmp (old->tests, add->tests)))
  771.            && old->test_elt_zero_int == add->test_elt_zero_int
  772.            && old->elt_zero_int == add->elt_zero_int
  773.            && old->test_elt_one_int == add->test_elt_one_int
  774.            && old->elt_one_int == add->elt_one_int
  775.            && old->test_elt_zero_wide == add->test_elt_zero_wide
  776.            && old->elt_zero_wide == add->elt_zero_wide
  777.            && old->veclen == add->veclen
  778.            && old->dupno == add->dupno
  779.            && old->opno == add->opno
  780.            && old->code == add->code
  781.            && old->enforce_mode == add->enforce_mode
  782.            && old->mode == add->mode)
  783.         {
  784.           /* If the additional test is not the same, split both nodes
  785.          into nodes that just contain all things tested before the
  786.          additional test and nodes that contain the additional test
  787.          and actions when it is true.  This optimization is important
  788.          because of the case where we have almost identical patterns
  789.          with different tests on target flags.  */
  790.  
  791.           if (old->c_test != add->c_test
  792.           && ! (old->c_test && add->c_test
  793.             && !strcmp (old->c_test, add->c_test)))
  794.         {
  795.           if (old->insn_code_number >= 0 || old->opno >= 0)
  796.             {
  797.               struct decision *split
  798.             = (struct decision *) xmalloc (sizeof (struct decision));
  799.  
  800.               mybcopy (old, split, sizeof (struct decision));
  801.  
  802.               old->success.first = old->success.last = split;
  803.               old->c_test = 0;
  804.               old->opno = -1;
  805.               old->insn_code_number = -1;
  806.               old->num_clobbers_to_add = 0;
  807.  
  808.               split->number = next_number++;
  809.               split->next = split->prev = 0;
  810.               split->mode = VOIDmode;
  811.               split->code = UNKNOWN;
  812.               split->veclen = 0;
  813.               split->test_elt_zero_int = 0;
  814.               split->test_elt_one_int = 0;
  815.               split->test_elt_zero_wide = 0;
  816.               split->tests = 0;
  817.               split->pred = -1;
  818.               split->dupno = -1;
  819.             }
  820.  
  821.           if (add->insn_code_number >= 0 || add->opno >= 0)
  822.             {
  823.               struct decision *split
  824.             = (struct decision *) xmalloc (sizeof (struct decision));
  825.  
  826.               mybcopy (add, split, sizeof (struct decision));
  827.  
  828.               add->success.first = add->success.last = split;
  829.               add->c_test = 0;
  830.               add->opno = -1;
  831.               add->insn_code_number = -1;
  832.               add->num_clobbers_to_add = 0;
  833.  
  834.               split->number = next_number++;
  835.               split->next = split->prev = 0;
  836.               split->mode = VOIDmode;
  837.               split->code = UNKNOWN;
  838.               split->veclen = 0;
  839.               split->test_elt_zero_int = 0;
  840.               split->test_elt_one_int = 0;
  841.               split->test_elt_zero_wide = 0;
  842.               split->tests = 0;
  843.               split->pred = -1;
  844.               split->dupno = -1;
  845.             }
  846.         }
  847.  
  848.           if (old->insn_code_number >= 0 && add->insn_code_number >= 0)
  849.         {
  850.           /* If one node is for a normal insn and the second is
  851.              for the base insn with clobbers stripped off, the
  852.              second node should be ignored.  */
  853.  
  854.           if (old->num_clobbers_to_add == 0
  855.               && add->num_clobbers_to_add > 0)
  856.             /* Nothing to do here.  */
  857.             ;
  858.           else if (old->num_clobbers_to_add > 0
  859.                && add->num_clobbers_to_add == 0)
  860.             {
  861.               /* In this case, replace OLD with ADD.  */
  862.               old->insn_code_number = add->insn_code_number;
  863.               old->num_clobbers_to_add = 0;
  864.             }
  865.           else
  866.             fatal ("Two actions at one point in tree");
  867.         }
  868.  
  869.           if (old->insn_code_number == -1)
  870.         old->insn_code_number = add->insn_code_number;
  871. #ifdef MPW_C
  872.               {
  873.             struct decision_head temp;
  874.         temp = merge_trees (old->success, add->success);
  875.         old->success = temp;
  876.           }
  877. #else
  878.           old->success = merge_trees (old->success, add->success);
  879. #endif
  880.           add = 0;
  881.           break;
  882.         }
  883.       /* Unless we have already found the best possible insert point,
  884.          see if this position is better.  If so, record it.  */
  885.  
  886.       if (best_merit != 0
  887.           && ((our_merit = position_merit (old, add_mode, add->code))
  888.           < best_merit))
  889.         best_merit = our_merit, best_position = old;
  890.  
  891.       if (! not_both_true (old, add, 0))
  892.         break;
  893.     }
  894.       /* If ADD was duplicate, we are done.  */
  895.       if (add == 0)
  896.     continue;
  897.  
  898.       /* Otherwise, find the best place to insert ADD.  Normally this is
  899.      BEST_POSITION.  However, if we went all the way to the top of
  900.      the list, it might be better to insert at the top.  */
  901.  
  902.       if (best_position == 0)
  903.     abort ();
  904.  
  905.       if (old == 0
  906.       && position_merit (NULL_PTR, add_mode, add->code) < best_merit)
  907.     {
  908.       add->prev = 0;
  909.       add->next = oldh.first;
  910.       oldh.first->prev = add;
  911.       oldh.first = add;
  912.     }
  913.  
  914.       else
  915.     {
  916.       add->prev = best_position;
  917.       add->next = best_position->next;
  918.       best_position->next = add;
  919.       if (best_position == oldh.last)
  920.         oldh.last = add;
  921.       else
  922.         add->next->prev = add;
  923.     }
  924.     }
  925.  
  926.   return oldh;
  927. }
  928.  
  929. /* Count the number of subnodes of HEAD.  If the number is high enough,
  930.    make the first node in HEAD start a separate subroutine in the C code
  931.    that is generated.
  932.  
  933.    TYPE gives the type of routine we are writing.
  934.  
  935.    INITIAL is non-zero if this is the highest-level node.  We never write
  936.    it out here.  */
  937.  
  938. static int
  939. break_out_subroutines (head, type, initial)
  940.      struct decision_head head;
  941.      enum routine_type type;
  942.      int initial;
  943. {
  944.   int size = 0;
  945.   struct decision *node, *sub;
  946.  
  947.   for (sub = head.first; sub; sub = sub->next)
  948.     size += 1 + break_out_subroutines (sub->success, type, 0);
  949.  
  950.   if (size > SUBROUTINE_THRESHOLD && ! initial)
  951.     {
  952.       head.first->subroutine_number = ++next_subroutine_number;
  953.       write_subroutine (head.first, type);
  954.       size = 1;
  955.     }
  956.   return size;
  957. }
  958.  
  959. /* Write out a subroutine of type TYPE to do comparisons starting at node
  960.    TREE.  */
  961.  
  962. static void
  963. write_subroutine (tree, type)
  964.      struct decision *tree;
  965.      enum routine_type type;
  966. {
  967.   int i;
  968.  
  969. #ifdef MPW
  970.   if (tree != 0 && tree->subroutine_number > 0 && tree->subroutine_number % 5 == 0) {
  971.     printf("#ifdef MPW\n#pragma segment \"insn_recog_SH%d\"\n#endif\n", tree->subroutine_number / 5);
  972.   }
  973. #endif
  974.  
  975.   if (type == SPLIT)
  976.     printf ("rtx\nsplit");
  977.   else
  978.     printf ("int\nrecog");
  979.  
  980.   if (tree != 0 && tree->subroutine_number > 0)
  981.     printf ("_%d", tree->subroutine_number);
  982.   else if (type == SPLIT)
  983.     printf ("_insns");
  984.  
  985.   printf (" (x0, insn");
  986.   if (type == RECOG)
  987.     printf (", pnum_clobbers");
  988.  
  989.   printf (")\n");
  990.   printf ("     register rtx x0;\n     rtx insn;\n");
  991.   if (type == RECOG)
  992.     printf ("     int *pnum_clobbers;\n");
  993.  
  994.   printf ("{\n");
  995.   printf ("  register rtx *ro = &recog_operand[0];\n");
  996.  
  997.   printf ("  register rtx ");
  998.   for (i = 1; i < max_depth; i++)
  999.     printf ("x%d, ", i);
  1000.  
  1001.   printf ("x%d;\n", max_depth);
  1002.   printf ("  %s tem;\n", type == SPLIT ? "rtx" : "int");
  1003.   write_tree (tree, "", NULL_PTR, 1, type);
  1004.   printf (" ret0: return %d;\n}\n\n", type == SPLIT ? 0 : -1);
  1005. }
  1006.  
  1007. /* This table is used to indent the recog_* functions when we are inside
  1008.    conditions or switch statements.  We only support small indentations
  1009.    and always indent at least two spaces.  */
  1010.  
  1011. static char *indents[]
  1012.   = {"  ", "  ", "  ", "   ", "    ", "     ", "      ", "       ",
  1013.      "\t", "\t ", "\t  ", "\t   ", "\t    ", "\t     ", "\t      ",
  1014.      "\t\t", "\t\t ", "\t\t  ", "\t\t   ", "\t\t    ", "\t\t     "};
  1015.  
  1016. /* Write out C code to perform the decisions in TREE for a subroutine of
  1017.    type TYPE.  If all of the choices fail, branch to node AFTERWARD, if
  1018.    non-zero, otherwise return.  PREVPOS is the position of the node that
  1019.    branched to this test.
  1020.  
  1021.    When we merged all alternatives, we tried to set up a convenient order.
  1022.    Specifically, tests involving the same mode are all grouped together,
  1023.    followed by a group that does not contain a mode test.  Within each group
  1024.    of the same mode, we also group tests with the same code, followed by a
  1025.    group that does not test a code.
  1026.  
  1027.    Occasionally, we cannot arbitrarily reorder the tests so that multiple
  1028.    sequence of groups as described above are present.
  1029.  
  1030.    We generate two nested switch statements, the outer statement for
  1031.    testing modes, and the inner switch for testing RTX codes.  It is
  1032.    not worth optimizing cases when only a small number of modes or 
  1033.    codes is tested, since the compiler can do that when compiling the
  1034.    resulting function.   We do check for when every test is the same mode
  1035.    or code.  */
  1036.  
  1037. void
  1038. write_tree_1 (tree, prevpos, afterward, type)
  1039.      struct decision *tree;
  1040.      char *prevpos;
  1041.      struct decision *afterward;
  1042.      enum routine_type type;
  1043. {
  1044.   register struct decision *p, *p1;
  1045.   register int depth = tree ? strlen (tree->position) : 0;
  1046.   enum machine_mode switch_mode = VOIDmode;
  1047.   RTX_CODE switch_code = UNKNOWN;
  1048.   int uncond = 0;
  1049.   char modemap[NUM_MACHINE_MODES];
  1050.   char codemap[NUM_RTX_CODE];
  1051.   int indent = 2;
  1052.   int i;
  1053.  
  1054.   /* One tricky area is what is the exact state when we branch to a
  1055.      node's label.  There are two cases where we branch: when looking at
  1056.      successors to a node, or when a set of tests fails.
  1057.  
  1058.      In the former case, we are always branching to the first node in a
  1059.      decision list and we want all required tests to be performed.  We
  1060.      put the labels for such nodes in front of any switch or test statements.
  1061.      These branches are done without updating the position to that of the
  1062.      target node.
  1063.  
  1064.      In the latter case, we are branching to a node that is not the first
  1065.      node in a decision list.  We have already checked that it is possible
  1066.      for both the node we originally tested at this level and the node we
  1067.      are branching to to be both match some pattern.  That means that they
  1068.      usually will be testing the same mode and code.  So it is normally safe
  1069.      for such labels to be inside switch statements, since the tests done
  1070.      by virtue of arriving at that label will usually already have been
  1071.      done.  The exception is a branch from a node that does not test a
  1072.      mode or code to one that does.  In such cases, we set the `retest_mode'
  1073.      or `retest_code' flags.  That will ensure that we start a new switch
  1074.      at that position and put the label before the switch. 
  1075.  
  1076.      The branches in the latter case must set the position to that of the
  1077.      target node.  */
  1078.  
  1079.  
  1080.   printf ("\n");
  1081.   if (tree && tree->subroutine_number == 0)
  1082.     {
  1083.       printf ("  L%d:\n", tree->number);
  1084.       tree->label_needed = 0;
  1085.     }
  1086.  
  1087.   if (tree)
  1088.     {
  1089.       change_state (prevpos, tree->position, 2);
  1090.       prevpos = tree->position;
  1091.     }
  1092.  
  1093.   for (p = tree; p; p = p->next)
  1094.     {
  1095.       enum machine_mode mode = p->enforce_mode ? p->mode : VOIDmode;
  1096.       int need_bracket;
  1097.       int wrote_bracket = 0;
  1098.       int inner_indent;
  1099.  
  1100.       if (p->success.first == 0 && p->insn_code_number < 0)
  1101.     abort ();
  1102.  
  1103.       /* Find the next alternative to p that might be true when p is true.
  1104.      Test that one next if p's successors fail.  */
  1105.  
  1106.       for (p1 = p->next; p1 && not_both_true (p, p1, 1); p1 = p1->next)
  1107.     ;
  1108.       p->afterward = p1;
  1109.  
  1110.       if (p1)
  1111.     {
  1112.       if (mode == VOIDmode && p1->enforce_mode && p1->mode != VOIDmode)
  1113.         p1->retest_mode = 1;
  1114.       if (p->code == UNKNOWN && p1->code != UNKNOWN)
  1115.         p1->retest_code = 1;
  1116.       p1->label_needed = 1;
  1117.     }
  1118.  
  1119.       /* If we have a different code or mode than the last node and
  1120.      are in a switch on codes, we must either end the switch or
  1121.      go to another case.  We must also end the switch if this
  1122.      node needs a label and to retest either the mode or code.  */
  1123.  
  1124.       if (switch_code != UNKNOWN
  1125.       && (switch_code != p->code || switch_mode != mode
  1126.           || (p->label_needed && (p->retest_mode || p->retest_code))))
  1127.     {
  1128.       enum rtx_code code = p->code;
  1129.  
  1130.       /* If P is testing a predicate that we know about and we haven't
  1131.          seen any of the codes that are valid for the predicate, we
  1132.          can write a series of "case" statement, one for each possible
  1133.          code.  Since we are already in a switch, these redundant tests
  1134.          are very cheap and will reduce the number of predicate called. */
  1135.  
  1136.       if (p->pred >= 0)
  1137.         {
  1138.           for (i = 0; i < NUM_RTX_CODE && preds[p->pred].codes[i] != 0; i++)
  1139.         if (codemap[(int) preds[p->pred].codes[i]])
  1140.           break;
  1141.  
  1142.           if (preds[p->pred].codes[i] == 0)
  1143.         code = MATCH_OPERAND;
  1144.         }
  1145.  
  1146.       if (code == UNKNOWN || codemap[(int) code]
  1147.           || switch_mode != mode
  1148.           || (p->label_needed && (p->retest_mode || p->retest_code)))
  1149.         {
  1150.           printf ("%s}\n", indents[indent - 2]);
  1151.           switch_code = UNKNOWN;
  1152.           indent -= 4;
  1153.         }
  1154.       else
  1155.         {
  1156.           if (! uncond)
  1157.         printf ("%sbreak;\n", indents[indent]);
  1158.  
  1159.           if (code == MATCH_OPERAND)
  1160.         {
  1161.           for (i = 0; i < NUM_RTX_CODE && preds[p->pred].codes[i] != 0; i++)
  1162.             {
  1163.               printf ("%scase ", indents[indent - 2]);
  1164.               print_code (preds[p->pred].codes[i]);
  1165.               printf (":\n");
  1166.               codemap[(int) preds[p->pred].codes[i]] = 1;
  1167.             }
  1168.         }
  1169.           else
  1170.         {
  1171.           printf ("%scase ", indents[indent - 2]);
  1172.           print_code (code);
  1173.           printf (":\n");
  1174.           codemap[(int) p->code] = 1;
  1175.         }
  1176.  
  1177.           switch_code = code;
  1178.         }
  1179.  
  1180.       uncond = 0;
  1181.     }
  1182.  
  1183.       /* If we were previously in a switch on modes and now have a different
  1184.      mode, end at least the case, and maybe end the switch if we are
  1185.      not testing a mode or testing a mode whose case we already saw.  */
  1186.  
  1187.       if (switch_mode != VOIDmode
  1188.       && (switch_mode != mode || (p->label_needed && p->retest_mode)))
  1189.     {
  1190.       if (mode == VOIDmode || modemap[(int) mode]
  1191.           || (p->label_needed && p->retest_mode))
  1192.         {
  1193.           printf ("%s}\n", indents[indent - 2]);
  1194.           switch_mode = VOIDmode;
  1195.           indent -= 4;
  1196.         }
  1197.       else
  1198.         {
  1199.           if (! uncond)
  1200.         printf ("      break;\n");
  1201.           printf ("    case %smode:\n", GET_MODE_NAME (mode));
  1202.           switch_mode = mode;
  1203.           modemap[(int) mode] = 1;
  1204.         }
  1205.  
  1206.       uncond = 0;
  1207.     }
  1208.  
  1209.       /* If we are about to write dead code, something went wrong.  */
  1210.       if (! p->label_needed && uncond)
  1211.     abort ();
  1212.  
  1213.       /* If we need a label and we will want to retest the mode or code at
  1214.      that label, write the label now.  We have already ensured that
  1215.      things will be valid for the test.  */
  1216.  
  1217.       if (p->label_needed && (p->retest_mode || p->retest_code))
  1218.     {
  1219.       printf ("%sL%d:\n", indents[indent - 2], p->number);
  1220.       p->label_needed = 0;
  1221.     }
  1222.  
  1223.       uncond = 0;
  1224.  
  1225.       /* If we are not in any switches, see if we can shortcut things
  1226.      by checking for identical modes and codes.  */
  1227.  
  1228.       if (switch_mode == VOIDmode && switch_code == UNKNOWN)
  1229.     {
  1230.       /* If p and its alternatives all want the same mode,
  1231.          reject all others at once, first, then ignore the mode.  */
  1232.  
  1233.       if (mode != VOIDmode && p->next && same_modes (p, mode))
  1234.         {
  1235.           printf ("  if (GET_MODE (x%d) != %smode)\n",
  1236.               depth, GET_MODE_NAME (p->mode));
  1237.           if (afterward)
  1238.         {
  1239.           printf ("    {\n");
  1240.           change_state (p->position, afterward->position, 6);
  1241.           printf ("      goto L%d;\n    }\n", afterward->number);
  1242.         }
  1243.           else
  1244.         printf ("    goto ret0;\n");
  1245.           clear_modes (p);
  1246.           mode = VOIDmode;
  1247.         }
  1248.  
  1249.       /* If p and its alternatives all want the same code,
  1250.          reject all others at once, first, then ignore the code.  */
  1251.  
  1252.       if (p->code != UNKNOWN && p->next && same_codes (p, p->code))
  1253.         {
  1254.           printf ("  if (GET_CODE (x%d) != ", depth);
  1255.           print_code (p->code);
  1256.           printf (")\n");
  1257.           if (afterward)
  1258.         {
  1259.           printf ("    {\n");
  1260.           change_state (p->position, afterward->position, indent + 4);
  1261.           printf ("    goto L%d;\n    }\n", afterward->number);
  1262.         }
  1263.           else
  1264.         printf ("    goto ret0;\n");
  1265.           clear_codes (p);
  1266.         }
  1267.     }
  1268.  
  1269.       /* If we are not in a mode switch and we are testing for a specific
  1270.      mode, start a mode switch unless we have just one node or the next
  1271.      node is not testing a mode (we have already tested for the case of
  1272.      more than one mode, but all of the same mode).  */
  1273.  
  1274.       if (switch_mode == VOIDmode && mode != VOIDmode && p->next != 0
  1275.       && p->next->enforce_mode && p->next->mode != VOIDmode)
  1276.     {
  1277.       mybzero (modemap, sizeof modemap);
  1278.       printf ("%sswitch (GET_MODE (x%d))\n", indents[indent], depth);
  1279.       printf ("%s{\n", indents[indent + 2]);
  1280.       indent += 4;
  1281.       printf ("%scase %smode:\n", indents[indent - 2],
  1282.           GET_MODE_NAME (mode));
  1283.       modemap[(int) mode] = 1;
  1284.       switch_mode = mode;
  1285.     }
  1286.  
  1287.       /* Similarly for testing codes.  */
  1288.  
  1289.       if (switch_code == UNKNOWN && p->code != UNKNOWN && ! p->ignore_code
  1290.       && p->next != 0 && p->next->code != UNKNOWN)
  1291.     {
  1292.       mybzero (codemap, sizeof codemap);
  1293.       printf ("%sswitch (GET_CODE (x%d))\n", indents[indent], depth);
  1294.       printf ("%s{\n", indents[indent + 2]);
  1295.       indent += 4;
  1296.       printf ("%scase ", indents[indent - 2]);
  1297.       print_code (p->code);
  1298.       printf (":\n");
  1299.       codemap[(int) p->code] = 1;
  1300.       switch_code = p->code;
  1301.     }
  1302.  
  1303.       /* Now that most mode and code tests have been done, we can write out
  1304.      a label for an inner node, if we haven't already. */
  1305.       if (p->label_needed)
  1306.     printf ("%sL%d:\n", indents[indent - 2], p->number);
  1307.  
  1308.       inner_indent = indent;
  1309.  
  1310.       /* The only way we can have to do a mode or code test here is if
  1311.      this node needs such a test but is the only node to be tested.
  1312.      In that case, we won't have started a switch.  Note that this is
  1313.      the only way the switch and test modes can disagree.  */
  1314.  
  1315.       if ((mode != switch_mode && ! p->ignore_mode)
  1316.       || (p->code != switch_code && p->code != UNKNOWN && ! p->ignore_code)
  1317.       || p->test_elt_zero_int || p->test_elt_one_int
  1318.       || p->test_elt_zero_wide || p->veclen
  1319.       || p->dupno >= 0 || p->tests || p->num_clobbers_to_add)
  1320.     {
  1321.       printf ("%sif (", indents[indent]);
  1322.  
  1323.       if (mode != switch_mode && ! p->ignore_mode)
  1324.         printf ("GET_MODE (x%d) == %smode && ",
  1325.             depth, GET_MODE_NAME (mode));
  1326.       if (p->code != switch_code && p->code != UNKNOWN && ! p->ignore_code)
  1327.         {
  1328.           printf ("GET_CODE (x%d) == ", depth);
  1329.           print_code (p->code);
  1330.           printf (" && ");
  1331.         }
  1332.  
  1333.       if (p->test_elt_zero_int)
  1334.         printf ("XINT (x%d, 0) == %d && ", depth, p->elt_zero_int);
  1335.       if (p->test_elt_one_int)
  1336.         printf ("XINT (x%d, 1) == %d && ", depth, p->elt_one_int);
  1337.       if (p->test_elt_zero_wide)
  1338.         printf (
  1339. #if HOST_BITS_PER_WIDE_INT == HOST_BITS_PER_INT
  1340.             "XWINT (x%d, 0) == %d && ",
  1341. #else
  1342.             "XWINT (x%d, 0) == %ld && ",
  1343. #endif
  1344.             depth, p->elt_zero_wide);
  1345.       if (p->veclen)
  1346.         printf ("XVECLEN (x%d, 0) == %d && ", depth, p->veclen);
  1347.       if (p->dupno >= 0)
  1348.         printf ("rtx_equal_p (x%d, ro[%d]) && ", depth, p->dupno);
  1349.       if (p->num_clobbers_to_add)
  1350.         printf ("pnum_clobbers != 0 && ");
  1351.       if (p->tests)
  1352.         printf ("%s (x%d, %smode)", p->tests, depth,
  1353.             GET_MODE_NAME (p->mode));
  1354.       else
  1355.         printf ("1");
  1356.  
  1357.       printf (")\n");
  1358.       inner_indent += 2;
  1359.     }
  1360.       else
  1361.     uncond = 1;
  1362.  
  1363.       need_bracket = ! uncond;
  1364.  
  1365.       if (p->opno >= 0)
  1366.     {
  1367.       if (need_bracket)
  1368.         {
  1369.           printf ("%s{\n", indents[inner_indent]);
  1370.           inner_indent += 2;
  1371.           wrote_bracket = 1;
  1372.           need_bracket = 0;
  1373.         }
  1374.  
  1375.       printf ("%sro[%d] = x%d;\n", indents[inner_indent], p->opno, depth);
  1376.     }
  1377.  
  1378.       if (p->c_test)
  1379.     {
  1380.       printf ("%sif (%s)\n", indents[inner_indent], p->c_test);
  1381.       inner_indent += 2;
  1382.       uncond = 0;
  1383.       need_bracket = 1;
  1384.     }
  1385.  
  1386.       if (p->insn_code_number >= 0)
  1387.     {
  1388.       if (type == SPLIT)
  1389.         printf ("%sreturn gen_split_%d (operands);\n",
  1390.             indents[inner_indent], p->insn_code_number);
  1391.       else
  1392.         {
  1393.           if (p->num_clobbers_to_add)
  1394.         {
  1395.           if (need_bracket)
  1396.             {
  1397.               printf ("%s{\n", indents[inner_indent]);
  1398.               inner_indent += 2;
  1399.             }
  1400.  
  1401.           printf ("%s*pnum_clobbers = %d;\n",
  1402.               indents[inner_indent], p->num_clobbers_to_add);
  1403.           printf ("%sreturn %d;\n",
  1404.               indents[inner_indent], p->insn_code_number);
  1405.  
  1406.           if (need_bracket)
  1407.             {
  1408.               inner_indent -= 2;
  1409.               printf ("%s}\n", indents[inner_indent]);
  1410.             }
  1411.         }
  1412.           else
  1413.         printf ("%sreturn %d;\n",
  1414.             indents[inner_indent], p->insn_code_number);
  1415.         }
  1416.     }
  1417.       else
  1418.     printf ("%sgoto L%d;\n", indents[inner_indent],
  1419.         p->success.first->number);
  1420.  
  1421.       if (wrote_bracket)
  1422.     printf ("%s}\n", indents[inner_indent - 2]);
  1423.     }
  1424.  
  1425.   /* We have now tested all alternatives.  End any switches we have open
  1426.      and branch to the alternative node unless we know that we can't fall
  1427.      through to the branch.  */
  1428.  
  1429.   if (switch_code != UNKNOWN)
  1430.     {
  1431.       printf ("%s}\n", indents[indent - 2]);
  1432.       indent -= 4;
  1433.       uncond = 0;
  1434.     }
  1435.  
  1436.   if (switch_mode != VOIDmode)
  1437.     {
  1438.       printf ("%s}\n", indents[indent - 2]);
  1439.       indent -= 4;
  1440.       uncond = 0;
  1441.     }
  1442.  
  1443.   if (indent != 2)
  1444.     abort ();
  1445.  
  1446.   if (uncond)
  1447.     return;
  1448.  
  1449.   if (afterward)
  1450.     {
  1451.       change_state (prevpos, afterward->position, 2);
  1452.       printf ("  goto L%d;\n", afterward->number);
  1453.     }
  1454.   else
  1455.     printf ("  goto ret0;\n");
  1456. }
  1457.  
  1458. static void
  1459. print_code (code)
  1460.      RTX_CODE code;
  1461. {
  1462.   register char *p1;
  1463.   for (p1 = GET_RTX_NAME (code); *p1; p1++)
  1464.     {
  1465.       if (*p1 >= 'a' && *p1 <= 'z')
  1466.     putchar (*p1 + 'A' - 'a');
  1467.       else
  1468.     putchar (*p1);
  1469.     }
  1470. }
  1471.  
  1472. static int
  1473. same_codes (p, code)
  1474.      register struct decision *p;
  1475.      register RTX_CODE code;
  1476. {
  1477.   for (; p; p = p->next)
  1478.     if (p->code != code)
  1479.       return 0;
  1480.  
  1481.   return 1;
  1482. }
  1483.  
  1484. static void
  1485. clear_codes (p)
  1486.      register struct decision *p;
  1487. {
  1488.   for (; p; p = p->next)
  1489.     p->ignore_code = 1;
  1490. }
  1491.  
  1492. static int
  1493. same_modes (p, mode)
  1494.      register struct decision *p;
  1495.      register enum machine_mode mode;
  1496. {
  1497.   for (; p; p = p->next)
  1498.     if ((p->enforce_mode ? p->mode : VOIDmode) != mode)
  1499.       return 0;
  1500.  
  1501.   return 1;
  1502. }
  1503.  
  1504. static void
  1505. clear_modes (p)
  1506.      register struct decision *p;
  1507. {
  1508.   for (; p; p = p->next)
  1509.     p->enforce_mode = 0;
  1510. }
  1511.  
  1512. /* Write out the decision tree starting at TREE for a subroutine of type TYPE.
  1513.  
  1514.    PREVPOS is the position at the node that branched to this node.
  1515.  
  1516.    INITIAL is nonzero if this is the first node we are writing in a subroutine.
  1517.  
  1518.    If all nodes are false, branch to the node AFTERWARD.  */
  1519.  
  1520. static void
  1521. write_tree (tree, prevpos, afterward, initial, type)
  1522.      struct decision *tree;
  1523.      char *prevpos;
  1524.      struct decision *afterward;
  1525.      int initial;
  1526.      enum routine_type type;
  1527. {
  1528.   register struct decision *p;
  1529.   char *name_prefix = (type == SPLIT ? "split" : "recog");
  1530.   char *call_suffix = (type == SPLIT ? "" : ", pnum_clobbers");
  1531.  
  1532.   if (! initial && tree->subroutine_number > 0)
  1533.     {
  1534.       printf (" L%d:\n", tree->number);
  1535.  
  1536.       if (afterward)
  1537.     {
  1538.       printf ("  tem = %s_%d (x0, insn%s);\n",
  1539.           name_prefix, tree->subroutine_number, call_suffix);
  1540.       if (type == SPLIT)
  1541.         printf ("  if (tem != 0) return tem;\n");
  1542.       else
  1543.         printf ("  if (tem >= 0) return tem;\n");
  1544.       change_state (tree->position, afterward->position, 2);
  1545.       printf ("  goto L%d;\n", afterward->number);
  1546.     }
  1547.       else
  1548.     printf ("  return %s_%d (x0, insn%s);\n",
  1549.         name_prefix, tree->subroutine_number, call_suffix);
  1550.       return;
  1551.     }
  1552.  
  1553.   write_tree_1 (tree, prevpos, afterward, type);
  1554.  
  1555.   for (p = tree; p; p = p->next)
  1556.     if (p->success.first)
  1557.       write_tree (p->success.first, p->position,
  1558.           p->afterward ? p->afterward : afterward, 0, type);
  1559. }
  1560.  
  1561.  
  1562. /* Assuming that the state of argument is denoted by OLDPOS, take whatever
  1563.    actions are necessary to move to NEWPOS.
  1564.  
  1565.    INDENT says how many blanks to place at the front of lines.  */
  1566.  
  1567. static void
  1568. change_state (oldpos, newpos, indent)
  1569.      char *oldpos;
  1570.      char *newpos;
  1571.      int indent;
  1572. {
  1573.   int odepth = strlen (oldpos);
  1574.   int depth = odepth;
  1575.   int ndepth = strlen (newpos);
  1576.  
  1577.   /* Pop up as many levels as necessary.  */
  1578.  
  1579.   while (strncmp (oldpos, newpos, depth))
  1580.     --depth;
  1581.  
  1582.   /* Go down to desired level.  */
  1583.  
  1584.   while (depth < ndepth)
  1585.     {
  1586.       if (newpos[depth] >= 'a' && newpos[depth] <= 'z')
  1587.     printf ("%sx%d = XVECEXP (x%d, 0, %d);\n",
  1588.         indents[indent], depth + 1, depth, newpos[depth] - 'a');
  1589.       else
  1590.     printf ("%sx%d = XEXP (x%d, %c);\n",
  1591.         indents[indent], depth + 1, depth, newpos[depth]);
  1592.       ++depth;
  1593.     }
  1594. }
  1595.  
  1596. static char *
  1597. copystr (s1)
  1598.      char *s1;
  1599. {
  1600.   register char *tem;
  1601.  
  1602.   if (s1 == 0)
  1603.     return 0;
  1604.  
  1605.   tem = (char *) xmalloc (strlen (s1) + 1);
  1606.   strcpy (tem, s1);
  1607.  
  1608.   return tem;
  1609. }
  1610.  
  1611. static void
  1612. mybzero (b, length)
  1613.      register char *b;
  1614.      register unsigned length;
  1615. {
  1616.   while (length-- > 0)
  1617.     *b++ = 0;
  1618. }
  1619.  
  1620. static void
  1621. mybcopy (in, out, length)
  1622.      register char *in, *out;
  1623.      register unsigned length;
  1624. {
  1625.   while (length-- > 0)
  1626.     *out++ = *in++;
  1627. }
  1628.  
  1629. static char *
  1630. concat (s1, s2)
  1631.      char *s1, *s2;
  1632. {
  1633.   register char *tem;
  1634.  
  1635.   if (s1 == 0)
  1636.     return s2;
  1637.   if (s2 == 0)
  1638.     return s1;
  1639.  
  1640.   tem = (char *) xmalloc (strlen (s1) + strlen (s2) + 2);
  1641.   strcpy (tem, s1);
  1642.   strcat (tem, " ");
  1643.   strcat (tem, s2);
  1644.  
  1645.   return tem;
  1646. }
  1647.  
  1648. char *
  1649. xrealloc (ptr, size)
  1650.      char *ptr;
  1651.      unsigned size;
  1652. {
  1653.   char *result = (char *) realloc (ptr, size);
  1654.   if (!result)
  1655.     fatal ("virtual memory exhausted");
  1656.   return result;
  1657. }
  1658.  
  1659. char *
  1660. xmalloc (size)
  1661.      unsigned size;
  1662. {
  1663.   register char *val = (char *) malloc (size);
  1664.  
  1665.   if (val == 0)
  1666.     fatal ("virtual memory exhausted");
  1667.   return val;
  1668. }
  1669.  
  1670. static void
  1671. fatal (s, a1, a2)
  1672.      char *s;
  1673. {
  1674.   fprintf (stderr, "genrecog: ");
  1675.   fprintf (stderr, s, a1, a2);
  1676.   fprintf (stderr, "\n");
  1677.   fprintf (stderr, "after %d definitions\n", next_index);
  1678.   exit (FATAL_EXIT_CODE);
  1679. }
  1680.  
  1681. /* More 'friendly' abort that prints the line and file.
  1682.    config.h can #define abort fancy_abort if you like that sort of thing.  */
  1683.  
  1684. void
  1685. fancy_abort ()
  1686. {
  1687.   fatal ("Internal gcc abort.");
  1688. }
  1689.  
  1690. int
  1691. main (argc, argv)
  1692.      int argc;
  1693.      char **argv;
  1694. {
  1695.   rtx desc;
  1696.   struct decision_head recog_tree;
  1697.   struct decision_head split_tree;
  1698.   FILE *infile;
  1699.   register int c;
  1700.  
  1701.   obstack_init (rtl_obstack);
  1702.   recog_tree.first = recog_tree.last = split_tree.first = split_tree.last = 0;
  1703.  
  1704.   if (argc <= 1)
  1705.     fatal ("No input file name.");
  1706.  
  1707.   infile = fopen (argv[1], "r");
  1708.   if (infile == 0)
  1709.     {
  1710.       perror (argv[1]);
  1711.       exit (FATAL_EXIT_CODE);
  1712.     }
  1713.  
  1714.   init_rtl ();
  1715.   next_insn_code = 0;
  1716.   next_index = 0;
  1717.  
  1718.   printf ("/* Generated automatically by the program `genrecog'\n\
  1719. from the machine description file `md'.  */\n\n");
  1720.  
  1721.   printf ("#include \"config.h\"\n");
  1722.   printf ("#include \"rtl.h\"\n");
  1723.   printf ("#include \"insn-config.h\"\n");
  1724.   printf ("#include \"recog.h\"\n");
  1725.   printf ("#include \"real.h\"\n");
  1726.   printf ("#include \"output.h\"\n");
  1727.   printf ("#include \"flags.h\"\n");
  1728.   printf ("\n");
  1729.  
  1730.   /* Read the machine description.  */
  1731.  
  1732.   while (1)
  1733.     {
  1734.       c = read_skip_spaces (infile);
  1735.       if (c == EOF)
  1736.     break;
  1737.       ungetc (c, infile);
  1738.  
  1739.       desc = read_rtx (infile);
  1740.       if (GET_CODE (desc) == DEFINE_INSN)
  1741.     recog_tree = merge_trees (recog_tree,
  1742.                   make_insn_sequence (desc, RECOG));
  1743.       else if (GET_CODE (desc) == DEFINE_SPLIT)
  1744.     split_tree = merge_trees (split_tree,
  1745.                   make_insn_sequence (desc, SPLIT));
  1746.       if (GET_CODE (desc) == DEFINE_PEEPHOLE
  1747.       || GET_CODE (desc) == DEFINE_EXPAND)
  1748.     next_insn_code++;
  1749.       next_index++;
  1750.     }
  1751.  
  1752.   printf ("\n\
  1753. /* `recog' contains a decision tree\n\
  1754.    that recognizes whether the rtx X0 is a valid instruction.\n\
  1755. \n\
  1756.    recog returns -1 if the rtx is not valid.\n\
  1757.    If the rtx is valid, recog returns a nonnegative number\n\
  1758.    which is the insn code number for the pattern that matched.\n");
  1759.   printf ("   This is the same as the order in the machine description of\n\
  1760.    the entry that matched.  This number can be used as an index into\n\
  1761.    entry that matched.  This number can be used as an index into various\n\
  1762.    insn_* tables, such as insn_templates, insn_outfun, and insn_n_operands\n\
  1763.    (found in insn-output.c).\n\n");
  1764. #ifdef APPLE_HAX
  1765.   printf ("   The third argument to recog is an optional pointer to an int.\n\
  1766.    If present, recog will accept a pattern if it matches except for\n\
  1767.    missing CLOBBER expressions at the end.  In that case, the value\n\
  1768.    pointed to by the optional pointer will be set to the number of\n");
  1769.   printf ("   CLOBBERs that need to be added (it should be initialized to zero by\n\
  1770.    the caller).  If it is set nonzero, the caller should allocate a\n\
  1771.    PARALLEL of the appropriate size, copy the initial entries, and call\n\
  1772.    add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.");
  1773. #else
  1774.   printf ("   The third argument to recog is an optional pointer to an int.\n\
  1775.    If present, recog will accept a pattern if it matches except for\n\
  1776.    missing CLOBBER expressions at the end.  In that case, the value\n\
  1777.    pointed to by the optional pointer will be set to the number of\n\
  1778.    CLOBBERs that need to be added (it should be initialized to zero by\n\
  1779.    the caller).  If it is set nonzero, the caller should allocate a\n\
  1780.    PARALLEL of the appropriate size, copy the initial entries, and call\n\
  1781.    add_clobbers (found in insn-emit.c) to fill in the CLOBBERs.");
  1782. #endif
  1783.   if (split_tree.first)
  1784.     printf ("\n\n   The function split_insns returns 0 if the rtl could not\n\
  1785.    be split or the split rtl in a SEQUENCE if it can be.");
  1786.  
  1787.   printf ("*/\n\n");
  1788.  
  1789.   printf ("rtx recog_operand[MAX_RECOG_OPERANDS];\n\n");
  1790.   printf ("rtx *recog_operand_loc[MAX_RECOG_OPERANDS];\n\n");
  1791.   printf ("rtx *recog_dup_loc[MAX_DUP_OPERANDS];\n\n");
  1792.   printf ("char recog_dup_num[MAX_DUP_OPERANDS];\n\n");
  1793.   printf ("#define operands recog_operand\n\n");
  1794.  
  1795.   next_subroutine_number = 0;
  1796.   break_out_subroutines (recog_tree, RECOG, 1);
  1797.   write_subroutine (recog_tree.first, RECOG);
  1798.  
  1799.   next_subroutine_number = 0;
  1800.   break_out_subroutines (split_tree, SPLIT, 1);
  1801.   write_subroutine (split_tree.first, SPLIT);
  1802.  
  1803.   fflush (stdout);
  1804.   exit (ferror (stdout) != 0 ? FATAL_EXIT_CODE : SUCCESS_EXIT_CODE);
  1805.   /* NOTREACHED */
  1806.   return 0;
  1807. }
  1808.